/*
  ABORD-GUI Main JS.
  @author: Luis M. Rodriguez <me at miguel dot weapps dot com>
*/


function load(rt,rq) {
	if ( location.hash ) {
		//window.location = rt + "?request=" + location.hash.substr(1);
		//return;
		rq = location.hash.substr(1);
	}
	include(rt+"lcl/en.js");
	AJAXer.start();
	BME.root = rt;
	BME.load();
	BME.request(rq);
}


/**
 * The BME var is the main object on GeBiX-GUI JS.  It has (or
 * can access to) all important data on the GeBiX-GUI.  The
 * Atributes and Methods on BME are described bellow.
 */
var BME = {
	// The Messages container over the global container.
	msgr:{},
	// The global container.
	cont:{},
	// The site's root
	root:"",
	// Current request
	current:"",
	/**
	 * The first call.
	 */
	load:function(){
		this.msgr = document.getElementById("messages_container");
		this.cont = document.getElementById("global_container");
		BME.checkReq();
	},
	checkReq:function(){
		if("#"+this.current!=location.hash){
			this.request(location.hash.substr(1));
		}
		setTimeout("BME.checkReq()",500);
	},
	/**
	 * Calls a request and puts it into the global_container.
	 * @param str req: The request on format 'module/path/to/request'
	 */
	request : function ( req ) {
		this.cont.innerHTML=req+"... ";
		location.hash="#"+req;
		AJAXer.request(req,this.cont.id);
		this.current=req;
	},
	/**
	 * Change the innerHTML of 'where' with 'what'
	 * @return bool: Everything went OK
	 */
	setInnerHtml:function(where,what) {
		if (where==this.cont.id) {
			this.cont.innerHTML=what;
			document.getElementById(where).innerHTML=what;
		} else {
			//var subcont = this.cont.getElementById(where);
			var subcont=document.getElementById(where);
			if (subcont) {
				subcont.innerHTML=what;
			} else {
				this.error(bmelcl.ERR_noObjOnContainer+where);
				return false;
			}
		}
		return true;
	},
	/**
	 * Error displayer function.
	 * @param str msg: The message to display.
	 */
	error:function(msg){
		var j=document.createElement("div");
		j.id="err_"+Math.random();
		j.className="Error";
		j.innerHTML=msg;
		this.msgr.appendChild(j);
		setTimeout("BME.msgClean('"+j.id+"')",1000*30);
	},
	/**
	 * Clean the messages line.
	 */
	msgClean:function(msg_id){
		var j = document.getElementById(msg_id);
		if (j) this.msgr.removeChild(j);
	}
};
